﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TRACv2.Common.Enumerations;
using TRACv2.Domain.DTO;
using TRACv2.Domain.Entities;
using TRACv2.UI.Facade;

//Author: Morgan William Davis III
//Project: TRAC Ticket Manager v2
//Date: 14 Mar 2014

namespace TRACv2.UI
{
    public partial class FormUpdateTicket : Form
    {
        #region [ Fields ]

        private readonly TRACFacade _controller;
        private TicketDTO _ticket;
        private Issue resolvedIssue;

        #endregion

        #region [ Constructor ]

        public FormUpdateTicket(TRACFacade controller, TicketDTO selectedTicket)
        {
            InitializeComponent();
            _controller = controller;
            _ticket = selectedTicket;
            cmbPriority.DataSource = Enum.GetValues(typeof(Priority));
            cmbStatus.DataSource = Enum.GetValues(typeof(Status));
            PopulateForm();
        }

        #endregion

        #region [ Control Events ]

        private void btnUpdateTicket_Click(object sender, EventArgs e)
        {
            var parsed = (Issue)Enum.Parse(typeof(Issue), txtIssue.Text, true);
            _ticket.Issue = parsed;
            _ticket.Priority = (Priority)cmbPriority.SelectedItem;
            _ticket.Status = (Status)cmbStatus.SelectedItem;
            _ticket.Note = txtNotes.Text;

            _controller.UpdateTicket(_ticket);
            DialogResult = DialogResult.OK;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure you want to permanently remove this ticket from the database?",
                "Confim Deletion", MessageBoxButtons.YesNo);
            {
                if (result == DialogResult.Yes)
                {
                    _controller.DeleteTicket(_ticket);
                    DialogResult = DialogResult.OK;
                }

                else
                {
                    MessageBox.Show("Deletion Cancelled", "The ticket will not be deleted.");
                }
            }

        }

        #endregion

        private void txtContact_Click(object sender, EventArgs e)
        {
            using (var form = new FormEmployeeList(_controller))
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtContact.Text = form.ReturnEmployee;
                    _ticket.Contact = form.EmployeeObject.ID;
                }
            }
        }

        private void txtCreator_Click(object sender, EventArgs e)
        {
            using (var form = new FormEmployeeList(_controller))
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtCreator.Text = form.ReturnEmployee;
                    _ticket.Creator = form.EmployeeObject.ID;
                }
            }
        }

        private void txtAssigned_Click(object sender, EventArgs e)
        {
            using (var form = new FormEmployeeList(_controller))
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtAssigned.Text = form.ReturnEmployee;
                    _ticket.Assigned = form.EmployeeObject.ID;
                }
            }
        }

        private void txtIssue_Click(object sender, EventArgs e)
        {
            using (var form = new FormIssueSelect())
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtIssue.Text = form.ReturnIssue;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        #region [ Methods ]

        /// <summary>
        /// Method uses data from the user selected ticket to populate the form with current information
        /// </summary>
        private void PopulateForm()
        {
            txtContact.Text = _controller.GetEmployeeByID(_ticket.Contact).ToString();
            txtCreator.Text = _controller.GetEmployeeByID(_ticket.Creator).ToString();
            txtAssigned.Text = _controller.GetEmployeeByID(_ticket.Assigned).ToString();
            txtIssue.Text = _ticket.Issue.ToString();
            cmbPriority.SelectedIndex = (int)_ticket.Priority;
            cmbStatus.SelectedIndex = (int)_ticket.Status;
            txtNotes.Text = _ticket.Note;
        }


        #endregion

    }
}
